package com.king.edu.service.impl;

import com.king.edu.dao.ResumeDAO;
import com.king.edu.pojo.Resume;
import com.king.edu.service.ResumeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName: ResumeServiceImpl
 * @Description: TODO
 * @Author: wangchangyong
 * @Date: 2020/4/16 10:42
 * @Version: 1.0.0
 **/
@Service
public class ResumeServiceImpl implements ResumeService {

    @Autowired
    ResumeDAO resumeDAO;

    @Override
    public List<Resume> findAll() {
        List<Resume> all = resumeDAO.findAll();
        return all;
    }

    @Override
    public Resume findById(Long id) {
        Optional<Resume> byId = resumeDAO.findById(id);
        return byId.get();
    }

    @Override
    public void add(Resume resume) {
        Long currentId;
        List<Resume> all = resumeDAO.findAll();
        if (all != null && all.size() > 0) {
            currentId = all.stream().max(Comparator.comparing(Resume::getId)).get().getId() + 1L;
        } else {
            currentId = 1L;
        }
        resume.setId(currentId);
        resumeDAO.save(resume);
    }

    @Override
    public void update(Resume resume) {
        Optional<Resume> byId = resumeDAO.findById(resume.getId());
        Resume oldResume = byId.get();
        BeanUtils.copyProperties(resume, oldResume);
        resumeDAO.save(oldResume);
    }

    @Override
    public void delete(Long id) {
        resumeDAO.deleteById(id);
    }

}
